package com.mowh.multithreading.balking.saver;

import java.io.IOException;
import java.util.Random;

/**
 * @author mowh1gd
 * @create 2023/11/14 16:40
 */
public class ChangerThread extends Thread {
    private final Data data;
    private final Random random = new Random();

    public ChangerThread(String name, Data data) {
        super(name);
        this.data = data;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; true; i++) {
                //模拟场景：修改数据但未手动保存这段随机时间内，自动保存saverThread进行了保存，时间一到Changer手动保存发现changed=false（即已经保存过了）则不再保存(balk)。
                data.change("No." + i);
                Thread.sleep(random.nextInt(1000));
                data.save();//手动保存
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
